package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.StartSetmealNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     *  添加套餐信息
     *  保证事务的一致性
     *  将获取的数据复制到Setmeal实体类中,然后利用前面类自动注入公共部分的参数:如更改时间,更改人,并且获取添加完后获得的setmealId
     *  添加完Setmeal数据后,还要添加SetmealDish关联表中的数据
     * @param setmealDTO
     */

    @Transactional
    public void saveSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        log.info("{}",setmeal.getCategoryId());
        setmealMapper.insert(setmeal);

        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishesList = setmealDTO.getSetmealDishes();
        if (setmealDishesList.size()>0&&setmealDishesList!=null) {
            setmealDishMapper.insertBatch(setmealDishesList, setmealId);
        }
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> pageList= setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(pageList.getTotal(),pageList.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal=setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);

        List<SetmealDish> setmealDishList=setmealDishMapper.selectBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 修改套餐信息和菜品信息
     * @param setmealVO
     */
    @Transactional
    public void update(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        Long setmealId = setmeal.getId();
        setmealMapper.update(setmeal);

        List<SetmealDish> setmealDishList = setmealVO.getSetmealDishes();
        setmealDishMapper.deleteBySetmealId(setmealId);
        if (setmealDishList.size()>0&&setmealDishList!=null){
            setmealDishMapper.insertBatch(setmealDishList,setmealId);
        }

    }

    @Override
    public void startOrStopSetMeal(Integer status, Long id) {

        Setmeal setmeal = Setmeal.builder().status(status).id(id).updateTime(LocalDateTime.now()).
                updateUser(BaseContext.getCurrentId()).build();
        //如果status要从0--1,就要判断id所在的套餐下的所有菜品是否有停售的
        if (status==1){
            //获取菜品状态为0的个数
            Integer count= setmealDishMapper.selectStatusCount(id);
            if (count>0){
                throw new StartSetmealNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
            else {
                setmealMapper.update(setmeal);
            }
        }else {
            setmealMapper.update(setmeal);
        }
    }

    /**
     * 批量删除套餐
     *      删除套餐的时候要判断套餐是否起售,
     *      如果删除套餐成功,也要将套餐下面的关联表数据删除
     *      ids是套餐们的ids
     * @param ids
     */
    @Transactional
    public void delete(Long[] ids) {
        Integer count=setmealMapper.getStatrCountByIds(ids);
        if (count>0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //删除套餐
        setmealMapper.delete(ids);
        //还要删除每个套餐对应的关系表
        for (Long ssetmealId : ids) {
            setmealDishMapper.deleteBySetmealId(ssetmealId);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }



}
